package com.lykj.llw.box.ui.activity.agent

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.app.BaseApplication
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.bus.FlowBus
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.data.bean.response.agent.AgentUserInfoResponse
import com.lykj.llw.box.databinding.ActAgentAccountBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 代理-账户信息
 * ================================================
 *
 * @author gosha
 * @date 2023/10/30-17:47
 */
class AgentAccountActivity : BaseActivity() {
    lateinit var binding: ActAgentAccountBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActAgentAccountBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()

        obtainAgentUserInfo()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "账户信息"
        binding.titleBar.tvRightOption.visibility = View.GONE
    }

    //获取代理用户信息
    private fun obtainAgentUserInfo() {
        RetrofitManager.instance.obtainAgentInfo(object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@AgentAccountActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean = GsonUtils.fromJson(responseBody, AgentUserInfoResponse::class.java)

                binding.etAccount.setText(
                    "${
                        if (bean.data.bankName.isNullOrEmpty()) "${
                            bean.data.tel.substring(
                                0,
                                3
                            )
                        }****${bean.data.tel.substring(7)}" else bean.data.bankName
                    }"
                )
                binding.etAccountInfo.setText(
                    "${
                        if (bean.data.bankAccount.isNullOrEmpty()) "${
                            bean.data.tel.substring(
                                0,
                                3
                            )
                        }****${bean.data.tel.substring(7)}" else bean.data.bankAccount
                    }"
                )

                binding.tvPhone.text =
                    "${bean.data.tel.substring(0, 3)}****${bean.data.tel.substring(7)}"
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@AgentAccountActivity, it)
                }
            }
        })
    }

    //修改代理用户信息
    private fun editAgentUserInfo() {
        clearBodyParam()
        initBodyParam()
        bodyParams["bankName"] = binding.etAccount.text.toString()
        bodyParams["bankAccount"] = binding.etAccountInfo.text.toString()
        RetrofitManager.instance.editAgentUserInfo(bodyParams,
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@AgentAccountActivity, d)
                }

                override fun specificHandle(responseBody: String) {
                    ToastUtils.showShort(this@AgentAccountActivity, "修改成功")
                    finish()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@AgentAccountActivity, it)
                    }
                }
            })
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.tvBtnSubmit.clickWithTrigger {
            editAgentUserInfo()
        }

        KeyboardUtils.registerSoftInputChangedListener(this) {
            if (it == 0) {
                binding.etAccount.clearFocus()
                binding.etAccountInfo.clearFocus()
            }
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}